package handler

import (
	"github.com/gin-gonic/gin"
	"go_shengshu_admin/enum"
	"go_shengshu_admin/model"
	"go_shengshu_admin/query"
	"go_shengshu_admin/resp"
	"go_shengshu_admin/service"
	"net/http"
)

type UserHandler struct {
	Srv *service.UserService
}

func (h *UserHandler) GetEntity(result model.User) resp.User {
	return resp.User{
		UserId:    result.UserId,
		Mobile:    result.Mobile,
		NickName:  result.NickName,
		Address:   result.NickName,
		IsDeleted: result.IsDeleted,
		IsLocked:  result.IsLocked,
	}
}

func (h *UserHandler) UserInfoHandler(c *gin.Context) {
	entity := resp.Entity{
		Code:      int(enum.OperateFail),
		Msg:       enum.OperateFail.String(),
		Data:      nil,
		Total:     0,
		TotalPage: 0,
	}
	userId := c.Param("id")
	if userId == "" {
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}
	u := model.User{
		UserId: userId,
	}
	result, err := h.Srv.Get(u)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}
	r := h.GetEntity(*result)
	entity = resp.Entity{
		Code:      int(enum.OperateOk),
		Msg:       enum.OperateOk.String(),
		Data:      r,
		Total:     0,
		TotalPage: 0,
	}
	c.JSON(http.StatusOK, gin.H{
		"entity": entity,
	})

}

func (h *UserHandler) UserListHandler(c *gin.Context) {
	var q query.ListQuery
	entity := resp.Entity{
		Code:      int(enum.OperateFail),
		Msg:       enum.OperateFail.String(),
		Data:      nil,
		Total:     0,
		TotalPage: 0,
	}
	if err := c.ShouldBindQuery(&q); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}

	list, err := h.Srv.List(&q)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}
	total, err := h.Srv.GetTotal()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}
	pageSize := q.PageSize
	if pageSize == 0 {
		pageSize = 5
	}
	page := 0
	if total != 0 {
		ret := int(total % pageSize)
		ret2 := int(total / pageSize)
		if ret == 0 {
			page = ret2
		} else {
			page = ret2 + 1
		}
	}

	var newList []*resp.User
	for _, item := range list {
		respUser := h.GetEntity(*item)
		newList = append(newList, &respUser)
	}

	entity = resp.Entity{
		Code:      int(enum.OperateOk),
		Msg:       enum.OperateOk.String(),
		Data:      list,
		Total:     total,
		TotalPage: page,
	}
	c.JSON(http.StatusOK, gin.H{
		"entity": entity,
	})
}

func (h *UserHandler) AddUserHandler(c *gin.Context) {
	entity := resp.Entity{
		Code:      int(enum.OperateFail),
		Msg:       enum.OperateFail.String(),
		Data:      nil,
		Total:     0,
		TotalPage: 0,
	}
	var user model.User
	if err := c.ShouldBindJSON(&user); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}

	result, err := h.Srv.Add(user)
	if err != nil {
		if err := c.ShouldBindJSON(&user); err != nil {
			c.JSON(http.StatusOK, gin.H{
				"entity": entity,
			})
		}
		return
	}
	r := h.GetEntity(*result)
	entity = resp.Entity{
		Code:      int(enum.OperateOk),
		Msg:       enum.OperateOk.String(),
		Data:      r,
		Total:     0,
		TotalPage: 0,
	}

	c.JSON(http.StatusOK, gin.H{
		"entity": entity,
	})
}

func (h *UserHandler) EditUserHandler(c *gin.Context) {
	entity := resp.Entity{
		Code:      int(enum.OperateFail),
		Msg:       enum.OperateFail.String(),
		Data:      nil,
		Total:     0,
		TotalPage: 0,
	}
	var user model.User
	if err := c.ShouldBindJSON(&user); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}
	b, err := h.Srv.Edit(user)
	if err != nil {
		entity.Msg = err.Error()
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}
	if !b {
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}
	entity.Code = int(enum.OperateOk)
	entity.Msg = enum.OperateOk.String()
	c.JSON(http.StatusOK, gin.H{
		"entity": entity,
	})
}

func (h *UserHandler) DeleteUserHandler(c *gin.Context) {
	entity := resp.Entity{
		Code:      int(enum.OperateFail),
		Msg:       enum.OperateFail.String(),
		Data:      nil,
		Total:     0,
		TotalPage: 0,
	}

	id := c.Param("id")
	if id == "" {
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}

	b, err := h.Srv.Delete(id)
	if err != nil {
		entity.Msg = err.Error()
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}

	if !b {
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}
	entity.Code = int(enum.OperateOk)
	entity.Msg = enum.OperateOk.String()
	c.JSON(http.StatusOK, gin.H{
		"entity": entity,
	})
}
